#ifndef DHWL_SOCKET_BYTESTRING_H
#define DHWL_SOCKET_BYTESTRING_H

#include "global.h"

using namespace std;

namespace ZY {

    class ByteString
    {
        public:
            ByteString( string &str ) : read_offset(0) { m_str.assign( str ); write_offset = str.length();};
            ByteString() : read_offset(0) , write_offset(0) {};
            virtual ~ByteString();

            uint64_t getUnsignedInt64();
            int64_t getInt64();

            ByteString *getUnsignedInt64( uint64_t & n) {n=getUnsignedInt64();return this;}
            ByteString *getInt64( int64_t & n) {n=getInt64();return this;}

            uint32_t getUnsignedInt32();
            int32_t getInt32();

            ByteString *getUnsignedInt32( uint32_t & n ) { n = getUnsignedInt32();return this;}
            ByteString *getInt32( int32_t &n ) { n = getInt32(); return this;}

            uint16_t getUnsignedInt16();
            int16_t getInt16();

            ByteString *getUnsignedInt16( uint16_t &n ) { n = getUnsignedInt16();return this;}
            ByteString *getInt16( int16_t &n ) { n = getInt16(); return this;}

            uint8_t getUnsignedInt8();
            int8_t getInt8();

            ByteString *getUnsignedInt8( uint8_t &n ) { n = getUnsignedInt8() ; return this;}
            ByteString *getInt8( int8_t &n ) { n = getInt8() ; return this;}

            ByteString * getCharArray( char *c , size_t size );
            ByteString * getString( string &str , size_t size );

            ByteString * getVarCharArray( char *c , size_t & size );
            ByteString * getVarString( string & str );

            ByteString *setUnsignedInt64( uint64_t n );
            ByteString *setInt64( int64_t n );

            ByteString *setUnsignedInt32( uint32_t n );
            ByteString *setInt32( int32_t n );

            ByteString *setUnsignedInt16( uint16_t n );
            ByteString *setInt16( int16_t n );

            ByteString *setUnsignedInt8( uint8_t n );
            ByteString *setInt8( int8_t n );

            ByteString *setCharArray( const char *c , size_t size );
            ByteString *setString( const string & str );

            ByteString *setVarCharArray( const char *c , size_t size );
            ByteString *setVarString( const string & str );

            ByteString *setByteString( ByteString & bs );

            string getDataString() {return m_str;}
            ByteString *setDataString( const string & str ) { clear(); m_str.assign( str ); write_offset=str.length();return this; }
            ByteString *setDataString( const char *c , size_t s ) { clear(); m_str.assign( c , s );write_offset=s;return this;}

            string & getDataStringQuote() { return m_str;}
            unsigned & getDataStringReadOffsetQuote() { return read_offset;}
            unsigned & getDataStringWriteOffsetQuote() { return write_offset;}

            void zip();

            void clear() {m_str.assign("");read_offset=write_offset=0;}

            size_t readLength() { return write_offset - read_offset;}

            void swap( ByteString & bs );
            void copyFrom( ByteString & bs );
            void copyTo( ByteString &bs );
        protected:
            string m_str;
            uint32_t read_offset;
            uint32_t write_offset;
        private:

    };

}
#endif // BYTESTRING_H
